/**
 * Name: Avinash Sridhar	G Number: G00861300
 */
package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) {
		try {

			InetAddress server_addr = InetAddress.getByName("localhost");

			//Prompt setup
			System.out.println("---------------------------------------");
			System.out.print("Enter search : ");


			Scanner userInput = new Scanner(System.in);
			while (userInput.hasNextLine())
			{
				//Prompt setup
				System.out.println("---------------------------------------");

				DatagramSocket client = new DatagramSocket();
				System.out.println("UDP Client is now connected to UDP Server at client port: "+ Integer.parseInt(args[0]));	
				DatagramPacket packet;	//to be loaded later

				String input = userInput.nextLine();
				if (input.equals("exit"))
				{
					System.out.println("UDP Client sending 'exit' status to UDP server...");

					packet = new DatagramPacket(input.getBytes(), input.length(), server_addr, Integer.parseInt(args[0]));
					client.send(packet);

					System.out.println("UDP Client exiting!");
					System.exit(0);
				}
				else
				{
					packet = new DatagramPacket(input.getBytes(), input.length(), server_addr, Integer.parseInt(args[0]));
					client.send(packet);

					System.out.println("UDP Client sent request to UDP Server...");

					byte[] buffer = new byte[4096];
					DatagramPacket resultPacket = new DatagramPacket(buffer, buffer.length);
					client.receive(resultPacket);
					String result = new String(resultPacket.getData(), 0, resultPacket.getLength());
					System.out.println("UDP Client has receieved the following results from UDP server: ");  		

					//Show all results
					System.out.println();
					System.out.println("******Search Results******");
					if (result.contains("%"))	//more than 1 result
					{
						List<String> results = Arrays.asList(result.split("%"));
						for (String eachResult : results)
						{
							String[] info = eachResult.split(" ");
							System.out.println("~~~~~");
							System.out.println("First Name: " + info[0]);
							System.out.println("Last Name: " + info[1]);
							System.out.println("Phone Number: " + info[2]);
							System.out.println("~~~~~");
						}
					}
					else
						System.out.println(result); //"No Match." result
					System.out.println("**************************");
					System.out.println();
				}
				client.close();
				packet = null;

				//Prompt setup
				System.out.println("---------------------------------------");
				System.out.print("Enter search : ");

			}

			userInput.close();
		} catch (IOException e) {
			//e.printStackTrace();
			System.out.println("ERROR: Client has no server at " + Integer.parseInt(args[0]) + " to connect to! Client is Exiting!");
			System.exit(0);
		}
	}

}
